package jmine.tec.report.impl.table.pdf;

import java.awt.Color;

import jmine.tec.report.api.table.ReportCell;
import jmine.tec.report.impl.table.RendererParameterInterface;

import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPTable;

/**
 * Classe abstrata de renderers de células em pdf
 * 
 * @author lundberg
 */
public abstract class AbstractCellPDFRenderer implements ReportCellPDFRenderer {

    private static final float GROUP_GRAYFILL = 1.0f;

    private static final float SUBGROUP_GRAYFILL = 0.80f;

    private static final String DEFAULT_FONT_NAME = "Helvetica";

    private static final int FONT_SIZE = 6;

    private static final int FONT_SIZE_TITLE = 9;

    /**
     * {@inheritDoc}
     */
    public void createCell(PdfPTable table, ReportCell cell) {
        RendererParameterInterface rpi = new RendererParameterInterface(cell);
        table.getDefaultCell().setHorizontalAlignment(this.getHorizontalAlignment(rpi));
        this.applyGroupParameters(table, cell, rpi);
        table.addCell(new Paragraph(this.getFormattedString(cell), this.getFont(rpi)));
    }

    /**
     * {@inheritDoc}
     */
    public void createCell(PdfPTable table, ReportCell cell, int horizontalAlignment) {
        RendererParameterInterface rpi = new RendererParameterInterface(cell);
        table.getDefaultCell().setHorizontalAlignment(horizontalAlignment);
        this.applyGroupParameters(table, cell, rpi);
        table.addCell(new Paragraph(this.getFormattedString(cell), this.getFont(rpi)));
    }

    /**
     * Aplica os parâmetros relativos a estilos de grupos e subgrupos
     * 
     * @param table table
     * @param cell cell
     * @param rpi rpi
     */
    private void applyGroupParameters(PdfPTable table, ReportCell cell, RendererParameterInterface rpi) {
        if (!rpi.isGroup()) {
            return;
        }
        Integer index = rpi.getGroupIndex();
        if (index == 0) {
            table.getDefaultCell().setGrayFill(GROUP_GRAYFILL);
        } else {
            table.getDefaultCell().setGrayFill(SUBGROUP_GRAYFILL);
        }
    }

    /**
     * Cria a fonte a ser usada na célula
     * 
     * @param rpi rpi
     * @return Font
     */
    protected Font getFont(RendererParameterInterface rpi) {
        return FontFactory.getFont(DEFAULT_FONT_NAME, this.getFontSize(rpi), this.getFontStyle(rpi), Color.BLACK);
    }

    /**
     * Retorna o tamanho da fonte que deve ser apresentada.
     * 
     * @param rpi rpi
     * @return int
     */
    private int getFontSize(RendererParameterInterface rpi) {
        if (rpi.getIsTitle()) {
            return FONT_SIZE_TITLE;
        } else {
            return FONT_SIZE;
        }
    }

    /**
     * Obtém o estilo da fonte a ser usada.
     * 
     * @param rpi rpi
     * @return font style
     */
    private int getFontStyle(RendererParameterInterface rpi) {
        if (rpi.getIsFooter() || rpi.getIsHeader() || rpi.getIsTitle() || rpi.isGroup()) {
            return Font.BOLD;
        } else {
            return Font.NORMAL;
        }
    }

    /**
     * O alinhamento horizontal que deve ser utilizado.
     * 
     * @param rpi rpi
     * @return int
     */
    protected abstract int getHorizontalAlignment(RendererParameterInterface rpi);
}
